

/**
 * 自动一个工具
 */
(function ($){
	$.defaults = {};
	
	$.defaults.Region = {
		provinceUrl:'',
		cityUrl:'',
		districtUrl:'',
		provinceId:'province',
		cityId:'city',
		districtId:'district',
		defaultProvince:'0',
		defaultCity:'0',
		defaultDistrict:'0',
		provinceCallback:null,
		cityCallback:null,
		districtCallback:null,
		rank:3//1只显示省2显示省市3显示省市地区
	};
	
	$.fn.Region = function(options){
		
		options = $.extend($.defaults.Region, options);
		var provinceHtml = '<select id="'+options.provinceId+'" name="'+options.provinceId+'"></select>';
		var cityHtml = '<select id="'+options.cityId+'" name="'+options.cityId+'"></select>';
		var districtHtml = '<select id="'+options.districtId+'" name="'+options.districtId+'"></select>';
		
		var that = $(this);
		that.append(provinceHtml);
		if(options.rank > 1){
			that.append(cityHtml);
		}
		if(options.rank > 2){
			that.append(districtHtml);
		}
		
		//绑定事件
		$(that).find('#'+options.provinceId).bind('change', function(e){
			var regionId = $(this).val();
			if(options.rank > 1){
				getCity(regionId);
			}
			if(options.provinceCallback){
				options.provinceCallback(regionId);
			}
		});
		if(options.rank > 1){
			$(that).find('#'+options.cityId).bind('change', function(e){
				var regionId = $(this).val();
				if(options.rank > 2){
					getDistrict(regionId);
				}
				if(options.cityCallback){
					options.cityCallback(regionId);
				}
			});
		}
		if(options.rank > 2){
			$(that).find('#'+options.districtId).bind('change', function(e){
				var regionId = $(this).val();
				if(options.districtCallback){
					options.districtCallback(regionId);
				}
			});
		}
		
		//loading
		openLoading();
		//创建省
		$.ajax({
			url:options.provinceUrl,
			type:'POST',
			data:{},
			dataType:'json',
			success:function(rst){
				$(that).find('#'+options.provinceId).html('');
				//添加默认值
				//$(that).find('#'+options.provinceId).append('<option value="-1">请选择省</option>');
				if(rst.status == 'OK'){
					var provinces = rst.message;
					var len = provinces.length;
					for(var i = 0; i < len; i++){
						var item = provinces[i];
						if(options.defaultProvince == item.regionId){
							$(that).find('#'+options.provinceId).append('<option value="'+item.regionId+'" selected="selected">'+item.regionName+'</option>');
						}
						else{
							$(that).find('#'+options.provinceId).append('<option value="'+item.regionId+'">'+item.regionName+'</option>');	
						}
					}
				}
				$(that).find('#'+options.provinceId).trigger('change');
			},
			complete:function(){
				closeLoading();
			}
		});
		
		//获取城市
		function getCity(regionId){
			openLoading();
			$.ajax({
				url:options.cityUrl,
				type:'POST',
				data:{regionId:regionId},
				dataType:'json',
				success:function(rst){
					$(that).find('#'+options.cityId).html('');
					//添加默认值
					//$(that).find('#'+options.cityId).append('<option value="-1">请选择市</option>');
					if(rst.status == 'OK'){
						var citys = rst.message;
						var len = citys.length;
						for(var i = 0; i < len; i++){
							var item = citys[i];
							if(options.defaultCity == item.regionId){
								$(that).find('#'+options.cityId).append('<option value="'+item.regionId+'" selected="selected">'+item.regionName+'</option>');
							}
							else{
								$(that).find('#'+options.cityId).append('<option value="'+item.regionId+'">'+item.regionName+'</option>');	
							}
						}
					}
					$(that).find('#'+options.cityId).trigger('change');
				},
				complete:function(){
					closeLoading();
				}
			});
		};
		
		//获取区县
		function getDistrict(regionId){
			openLoading();
			$.ajax({
				url:options.districtUrl,
				type:'POST',
				data:{regionId:regionId},
				dataType:'json',
				success:function(rst){
					$(that).find('#'+options.districtId).html('');
					//添加默认值
					//$(that).find('#'+options.districtId).append('<option value="-1">请选择区县</option>');
					if(rst.status == 'OK'){
						var districts = rst.message;
						var len = districts.length;
						for(var i = 0; i < len; i++){
							var item = districts[i];
							if(options.defaultDistrict == item.regionId){
								$(that).find('#'+options.districtId).append('<option value="'+item.regionId+'" selected="selected">'+item.regionName+'</option>');
							}
							else{
								$(that).find('#'+options.districtId).append('<option value="'+item.regionId+'">'+item.regionName+'</option>');	
							}
						}
						$(that).find('#'+options.districtId).trigger('change');
					}
				},
				complete:function(){
					closeLoading();
				}
			});
		}
		
	}
	
})(jQuery); 

/**
 * 日期时间格式化
 */
Date.prototype.format = function(format) 
{ 
	var o = 
	{ 
		"M+" : this.getMonth()+1, //month 
		"d+" : this.getDate(), //day 
		"h+" : this.getHours(), //hour 
		"m+" : this.getMinutes(), //minute 
		"s+" : this.getSeconds(), //second 
		"q+" : Math.floor((this.getMonth()+3)/3), //quarter 
		"S" : this.getMilliseconds() //millisecond 
	} 
	
	if(/(y+)/.test(format)) 
	{ 
		format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
	} 
	
	for(var k in o) 
	{ 
		if(new RegExp("("+ k +")").test(format)) 
		{ 
			format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); 
		} 
	} 
	return format; 
} 
